package com.ChenYaRou.www.controller.servlet;

import com.ChenYaRou.www.controller.PersonalHomePageServletController;
import com.ChenYaRou.www.po.Collection;
import com.ChenYaRou.www.po.DetailedUser;
import com.ChenYaRou.www.po.Orders;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static com.ChenYaRou.www.constant.Constant.*;
import static com.ChenYaRou.www.constant.False.EMPTY;
import static com.ChenYaRou.www.constant.HomePage.*;

/**
 * @ClassName HomePageDetailedUserServlet
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/4 1:14
 * @Version 1.0
 **/
public class HomePageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        PrintWriter out = resp.getWriter();
        JSONObject json = JSON.parseObject(req.getParameter("json"));
        System.out.println(json);
        Integer id = Integer.parseInt(String.valueOf(json.get("id")));
        Object mode = json.get(MODE);
        PersonalHomePageServletController personalHomePageServletController=new PersonalHomePageServletController();
        if(DETAIL_USER.equals(mode)){
            out.write(personalHomePageServletController.personalHomePageServletDetailUser(id));
        }else if (COLLECTION.equals(mode)){
            out.write(personalHomePageServletController.personalHomePageServletCollection(
                    id, json.getInteger("shopId")));
        }else if(ORDERS.equals(mode)){
            Orders orders = new Orders();
            orders.setBuyerId(id);
            orders.setState(json.getString("state"));
            out.write(personalHomePageServletController.personalHomePageOrders(orders));
        }else if(TICKETS.equals(mode)){
            out.write(personalHomePageServletController.personalHomePageTicket(id));
        }else if(CARS.equals(mode)){
            out.write(personalHomePageServletController.personalHomePageCar(id));
        }else if(UPDATE_DETAILED_USER.equals(mode)){
            DetailedUser detailedUser=new DetailedUser();
            if(!EMPTY.equals(json.getString(TELEPHONE))){
                detailedUser.setTelephone(json.getString("telephone"));
            }
            if(!EMPTY.equals(json.getString(NEW_DISTRICT))){
                detailedUser.setDistrictName(json.getString("newDistrict"));
            }
            if(!EMPTY.equals(json.getString(NEW_STREET))){
                detailedUser.setStreetName(json.getString("newStreet"));
            }
            if(!EMPTY.equals(json.getString(ADDRESS))){
                detailedUser.setDetailedAddress(json.getString("address"));
            }
            if(json.getFloat(MONEY)!=null){
                detailedUser.setMoney(json.getFloat("money"));
            }
            out.write(personalHomePageServletController.updateDetailUser(detailedUser,id));
        }else if(UPDATE_COLLECTION.equals(mode)){
            out.write(personalHomePageServletController.updateCollection(
                    new Collection(json.getInteger("userId"), json.getInteger("shopId"), SHOW)));
        }else if(UPDATE_ORDERS.equals(mode)){
            Orders orders=new Orders();
            orders.setId(json.getInteger("ordersId"));
            orders.setState(json.getString("state"));
            System.out.println(orders);
            out.write(personalHomePageServletController.updateOrders(orders));
        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req,resp);
    }
}
